/**
 * Copyright (c) 2008-2011 FeiLong, Inc. All Rights Reserved.
 * <p/>
 * This software is the confidential and proprietary information of FeiLong
 * Network Technology, Inc. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with FeiLong.
 * <p/>
 * FeiLong MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
 * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 * PURPOSE, OR NON-INFRINGEMENT. FeiLong SHALL NOT BE LIABLE FOR ANY DAMAGES
 * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
 * THIS SOFTWARE OR ITS DERIVATIVES.
 * <p/>
 */
package com.feilong.commons.core.net;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * 飞龙互联网协议 (ip) 地址相关操作
 * 
 * @author <a href="mailto:venusdrogon@163.com">金鑫</a>
 * @version 1.0 2011-1-12 上午09:57:10
 */
public class InetAddressUtil{

	/**
	 * 在给定主机名的情况下确定主机的 ip地址。 <br>
	 * 主机名可以是机器名（如 "java.sun.com"），也可以是其 ip 地址的文本表示形式。<br>
	 * 
	 * <pre>
	 * 如果提供字面值 ip地址，则仅检查地址格式的有效性。 
	 * 对于以字面值 ipv6 地址指定的 host，在 RFC 2732 中定义的形式或在 RFC 2373 中定义的字面值 ipv6 地址格式都可以接受。ipv6 范围地址也受支持。
	 * 如果主机为 null，则返回表示回送接口地址的 InetAddress。
	 * </pre>
	 * 
	 * @param host
	 *            指定的主机，或 null。
	 * @return 给定主机名的 ip 地址。
	 */
	public static InetAddress getInetAddress(String host){
		// byte的范围是－128～127，所以0，1不用
		// byte[] ips = new byte[] { (byte) 127, (byte) 0, 0, 1 };
		try{
			InetAddress inetAddress = InetAddress.getByName(host);
			return inetAddress;
		}catch (UnknownHostException e){
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 将域名转成IP
	 * 
	 * <pre>
	 * convertDomainNameToIpAddress("www.e-lining.com")----->"210.14.70.88"
	 * </pre>
	 * 
	 * @param domainName
	 *            域名,不带参数 如www.e-lining.com
	 * @return 域名对应的ip地址
	 */
	public static String convertDomainNameToIpAddress(String domainName){
		InetAddress inetAddress = getInetAddress(domainName);
		return inetAddress.getHostAddress();
	}
}
